//
//  JXMinePostsCtl.m
//  JiaoX
//
//  Created by yinqy on 15/2/27.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXMinePostsCtl.h"
#import "ForumDataTool.h"
#import "MJRefresh.h"
#import "MineInteractResult.h"
#import "MinePostItemCell.h"
#import "JXNewTopicCtl.h"
#import "JXPostsDetailCtl.h"
#import "BBSPosts.h"
#import "MinePostsHeaderView.h"


@interface JXMinePostsCtl ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *postFrameArray;
@property (nonatomic,strong)NSArray *modelArray;
@property (nonatomic,strong)MinePostsHeaderView *headerView;

@end


@implementation JXMinePostsCtl

-(instancetype)initWithModelList:(NSArray *)array{
    self = [super init];
    if (self) {
        self.modelArray = array;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"我的帖子"];
    [self.view setBackgroundColor:JXGlobalColorBg];
    self.postFrameArray = [NSMutableArray array];
    
    self.headerView = [[MinePostsHeaderView alloc]init];
    [self.headerView.userInfoView setUserInfo:[Utils currentUserInfo]];
    [self.headerView setY:NavHeight];
    [self.view addSubview:self.headerView];
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headerView.frame), JXScreenW, JXScreenH-CGRectGetMaxY(self.headerView.frame))];
    
    [tableView setDelegate:self];
    [tableView setDataSource:self];
    [tableView setSeparatorColor:JXColor(242, 242, 242)];
    [self.view addSubview:tableView];
    [tableView setTableFooterView:[[UIView alloc]init]];
    self.tableView = tableView;

    [self.tableView addHeaderWithTarget:self action:@selector(headerRefresh)];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefresh)];
    [self.tableView headerBeginRefreshing];
    
    [UserDefaultsHelper setInteger:0 forKey:KEY_MYPOSTS_NOTI_NUM];
}


-(void)headerRefresh{
    [self requestDataWithLastTime:@""];
}

-(void)footerRefresh{
    if (self.postFrameArray.count>0) {
        MinePostItemFrame *frame = [self.postFrameArray lastObject];
        [self requestDataWithLastTime:frame.interact.createTime];
    }else{
        [self requestDataWithLastTime:@""];
    }
}

-(void)refreshComplete{
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
}

-(void)requestDataWithLastTime:(NSString *)lastTime{
    [ForumDataTool getMineInteractDataWithUrl:[NSString stringWithFormat:@"%@forum/myPosts",BASE_URL] lastTime:lastTime replyType:@"-1" success:^(MineInteractResult *result) {
        [self refreshComplete];
        if ([Utils isBlankString:result.data]) {
            [self convertDataToFrame:result lastTime:lastTime];
        }else{
            [DialogUtils showTipsWithView:self.navigationController.view String:result.data];
        }
    } failure:^(NSError *error) {
        [self refreshComplete];
        [DialogUtils showTipsWithView:self.navigationController.view String:@"加载失败"];
    }];
}
-(void)convertDataToFrame:(MineInteractResult *)result lastTime:(NSString *)lastTime{
    
    if (ISBLANK(lastTime)) {
        [self.headerView.userInfoView setUrl:result.url];
        if (!ISBLANK(result.sentence)) {
            [self.headerView setSentence:result.sentence];
            [self.tableView setY:CGRectGetMaxY(self.headerView.frame)];
            [self.tableView setHeight:self.view.height-self.tableView.y];
        }
    }
    if ((result.interactList ==nil || result.interactList.count==0) && self.postFrameArray.count==0) {
        [self showNullView];
        return;
    }else{
        if ([Utils isBlankString:lastTime]) {
            [self.postFrameArray removeAllObjects];
        }
        for (int i = 0; i<result.interactList.count; i++) {
            MineInteract *interact = result.interactList[i];
            MinePostItemFrame *frame = [[MinePostItemFrame alloc]init];
            [frame setInteract:interact];
            [self.postFrameArray addObject:frame];
        }
        [self.tableView reloadData];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.postFrameArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MinePostItemCell *cell = [MinePostItemCell cellWithTableView:tableView];
    [cell setMinePostItemFrame:self.postFrameArray[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    MinePostItemFrame *frame = self.postFrameArray[indexPath.row];
    return frame.frame.size.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MinePostItemFrame *frame = self.postFrameArray[indexPath.row];
    if (frame.interact.status==1) {
        //审核通过
        //跳转到帖子详情
        BBSPosts *bbs = [BBSPosts new];
        bbs.postsId = frame.interact.postsId;
        bbs.title = frame.interact.postsTitle;
        bbs.nickName = [UserDefaultsHelper stringForKey:KEY_NICK_NAME];
        bbs.userGender = [UserDefaultsHelper stringForKey:KEY_USER_GENDER];
        bbs.level = [UserDefaultsHelper integerForKey:KEY_USER_LEVEL];
        JXPostsDetailCtl *ctl = [[JXPostsDetailCtl alloc]initWithBBSPosts:bbs];
        [self.navigationController pushViewController:ctl animated:YES];
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)showNullView{
    [self.tableView removeFromSuperview];
    //添加nullview
    
    UIView *nullView = [[UIView alloc]init];
    
    UILabel *label = [[UILabel alloc]init];
    [label setTextColor:[UIColor blackColor]];
    [label setText:@"你还没有发过帖哦"];
    [label setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:16]]];
    [label setSize:[label.text sizeWithFont:label.font]];
    [nullView addSubview:label];
    
    
    UIButton *btn = [[UIButton alloc]init];
    [btn addTarget:self action:@selector(sendPosts) forControlEvents:UIControlEventTouchUpInside];
    [btn setFrame:CGRectMake(0, CGRectGetMaxY(label.frame)+10, label.width,[DimensUtils getScaleX:34])];
    [btn.layer setCornerRadius:5];
    [btn.layer setBorderWidth:1];
    [btn.layer setMasksToBounds:YES];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:15]]];
    [btn.layer setBorderColor:[JXThemeColor CGColor]];
    [btn setTitle:@"发帖" forState:UIControlStateNormal];
    [btn setTitleColor:JXThemeColor forState:UIControlStateNormal];
    [nullView addSubview:btn];
    
    nullView.size = CGSizeMake(btn.width, CGRectGetMaxY(btn.frame));
    nullView.center = self.view.center;
    [self.view addSubview:nullView];
}

-(void)sendPosts{
    JXNewTopicCtl* ctl =  [[JXNewTopicCtl alloc]initWithModelList:self.modelArray];
    [self.navigationController pushViewController:ctl animated:YES];
}

-(BOOL)shouldLogin{
    return YES;
}
@end
